package com.yw.ffmpeg;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.yw.ffmpeg.decoder.PcmDecoder;
import com.yw.ffmpeg.player.AudioTrackPlayer;

import java.io.File;

/**
 * @ProjectName: AndroidFFMpeg
 * @Package: com.yw.ffmpeg
 * @ClassName: 使用AudioTrack播放PCM文件
 * @Description: 使用AudioTrack播放PCM文件
 * @Author: wei.yang
 * @CreateDate: 2021/8/20 10:02
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/8/20 10:02
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class UserAudioTrackPlayPCMActivity extends BaseActivity {
    //选择文件
    private Button btnChoiceVideo;
    //选择文件的路径
    private TextView tvVideoFilePath;
    //生成pcm文件
    private Button btnGeneratePCM;
    //生成的pcm路径
    private TextView tvPcmPath;
    //使用AudioTrack播放pcm裸数据
    private Button btnUseAudioTrackPaly;
    private PcmDecoder pcmDecoder;
    private String orgPath = null;

    /**
     * 测试步骤
     * 1.选择一个视频文件
     * 2.将视频文件和音频文件分离
     * 3.将分离后的音频文件解码并保存为PCM
     * 4.利用AudioTrack播放PCM
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_audio_track_play_pcm);
        initViews();
        pcmDecoder = new PcmDecoder();
    }

    private void initViews() {
        btnChoiceVideo = findViewById(R.id.btnChoiceVideo);
        btnChoiceVideo.setOnClickListener(v -> {
            choiceVideo();
        });
        tvVideoFilePath = findViewById(R.id.tvVideoFilePath);
        btnGeneratePCM = findViewById(R.id.btnGeneratePCM);
        btnGeneratePCM.setOnClickListener(v -> {
            if (pcmDecoder != null) {
                if (orgPath != null) {
//                    new Thread() {
//                        @Override
//                        public void run() {
                    try {
                        int ret = pcmDecoder.init(orgPath, "/mnt/sdcard/110119.pcm");
                        if (ret >= 0) {
                            pcmDecoder.decode();
                            pcmDecoder.destroy();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                        }
//                    }.start();


                }
            }
        });
        tvPcmPath = findViewById(R.id.tvPcmPath);
        btnUseAudioTrackPaly = findViewById(R.id.btnUseAudioTrackPaly);
        btnUseAudioTrackPaly.setOnClickListener(v -> {
            AudioTrackPlayer.getAudioTrackPlayer().start(new File("/mnt/sdcard/110119.pcm"));
        });

    }

    @Override
    public void videoPathCallback(String videoPath) {
        orgPath = videoPath;
    }

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pcmDecoder != null) {
            pcmDecoder.destroy();
        }
        AudioTrackPlayer.getAudioTrackPlayer().release();
    }





}
